//
//  StatusCell.swift
//  Weibo10
//
//  Created by male on 15/10/18.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

/// 微博 Cell 中控件的间距数值
let StatusCellMargin: CGFloat = 12
/// 微博头像的宽度
let StatusCellIconWidth: CGFloat = 35

/// 微博 Cell
class StatusCell: UITableViewCell {

    /// 微博视图模型
    var viewModle: StatusViewModel? {
        didSet {
            topView.viewModle = viewModle
            
            contentLable.text = viewModle?.status.text
            
            pictureView.viewModel = viewModle
    
//            // 动态修改Cell行高,在实际开发中如果动态的改变约束,可能会导致计算行高有误
            pictureView.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(pictureView.bounds.width)
                make.height.equalTo(pictureView.bounds.height)
                
                // 若果没有图片改变图片上部的约束,调整Cell的行高
//                if viewModle?.thumbnailUrls?.count == nil {
//                    make.top.equalTo(contentLable.snp_bottom).offset(0)
//                }
//                let offset = viewModle?.thumbnailUrls?.count > 0 ? StatusCellMargin : 0
//                    make.top.equalTo(contentLable.snp_bottom).offset(offset)
            }
        }
    }
    /// 计算Cell的高度
    func rowHeight(viewModel: StatusViewModel) -> CGFloat {
        self.viewModle = viewModel
        if viewModel.status.pic_urls == nil {
            
        }
        contentView.layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
    }
    
    // MARK: - 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        //修改Cell的选择样式
        selectionStyle = .None
    }

     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
    
    // MARK: - 懒加载控件
    /// 顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    /// 微博正文标签
    lazy var contentLable: UILabel = UILabel(title: "微博正文", fontSize: 15, color: UIColor.darkGrayColor(), screenInset: StatusCellMargin)
    /// 配图视图
    lazy var pictureView: SYLJStatusCellPictureView = SYLJStatusCellPictureView()
    /// 底部视图
    lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
}

// MARK: - 设置界面
extension StatusCell {
    
    func setupUI() {
        // 1. 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLable)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 2. 自动布局
        // 1> 顶部视图
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
        }
        // 2> 内容标签
        contentLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(topView.snp_left).offset(StatusCellMargin)
            
        }
        // 4> 底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            
        }
        
    }
}
